package com.wb3.meta.handler;

import com.wb3.meta.api.core.service.UsersService;
import com.wb3.meta.api.core.vo.ReqUsersVo;
import com.wb3.meta.api.core.vo.ResUsersVo;
import com.wb3.meta.builder.TextBuilder;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author <a href="https://github.com/binarywang">Binary Wang</a>
 */
@Component
public class SubscribeHandler extends AbstractHandler {
    @Autowired
    private UsersService usersService;
    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                    Map<String, Object> context, WxMpService weixinService,
                                    WxSessionManager sessionManager) throws WxErrorException {

        this.logger.info("新关注用户 OPENID: " + wxMessage.getFromUser());
        // TODO 可以添加关注用户到本地数据库
        ReqUsersVo reqUsersVo = new ReqUsersVo();
        reqUsersVo.setMpOpenId(wxMessage.getFromUser());
        ResUsersVo resUsersVo = usersService.getResModelByConditions(reqUsersVo);
        if (resUsersVo == null) {
//                    reqUsersVo.setNickName(userWxInfo.getNickname());
            usersService.saveOrUpdate(reqUsersVo);
        }
        // 获取微信用户基本信息
//        try {
//            WxMpUser userWxInfo = weixinService.getUserService()
//                .userInfo(wxMessage.getFromUser(), null);
//            if (userWxInfo != null) {
//            }
//        } catch (WxErrorException e) {
//            if (e.getError().getErrorCode() == 48001) {
//                this.logger.info("该公众号没有获取用户信息权限！");
//            }
//        }


        WxMpXmlOutMessage responseResult = null;
        try {
            responseResult = this.handleSpecial(wxMessage);
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
        }

        if (responseResult != null) {
            return responseResult;
        }

        String subMsg = "感谢关注超级崽崽！\n" +
                "回复以下数字编号免费领取崽崽（只回复数字即可），每人限领1只哦\n" +
                "1：温柔男友\n" +
                "2：可爱女儿\n" +
                "3：榴莲同学\n" +
                "4：香蕉同学\n" +
                "5：霸道总裁\n" +
                "6：答案之书\n" +
                "7：成语接龙\n" +
                "\n" +
                "点击「领取福利」，完成任务可领养更多崽崽！";

        try {
            return new TextBuilder().build(subMsg, wxMessage, weixinService);
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
        }

        return null;
    }

    /**
     * 处理特殊请求，比如如果是扫码进来的，可以做相应处理
     */
    private WxMpXmlOutMessage handleSpecial(WxMpXmlMessage wxMessage)
        throws Exception {
        //TODO
        return null;
    }

}
